<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

$cache_time=2;
$OJ_CACHE_SHARE=false;
require_once("config/path_config.php");
require_once('./include/db_info.inc.php');
require_once('./include/setlang.php');
$view_title= "$MSG_STATUS";
require_once("./include/const.inc.php");
require_once("./config/sys_config.php");


// 参数错误或者用户未登陆
if ( !isset($_GET['solution_id']) || !isset($_SESSION['user_id']) ) {
    echo "0, 0, 0";
    exit(0);
}

$solution_id=0;
if ( isset($_GET['solution_id']) ){
    $solution_id=intval($_GET['solution_id']);
}

if($OJ_MEMCACHE){
	$sql="select * from solution where solution_id=$solution_id  LIMIT 1";
	require("./include/memcache.php");
	$result = mysql_query_cache($sql);
}else {
	$sql="select *, TIMESTAMPDIFF(SECOND, judgetime, NOW()) as waittime from solution where solution_id=? LIMIT 1";
	$result = pdo_query($sql, $solution_id);
}

// 未查询到solution信息
if ( !isset($result) ) {
    echo "0, 0, 0";
    exit(0);
}

$row=$result[0];

$res = $row['result'];
$runtime = $row['time'];
// 间接解决无法判断超时的情况******
if ( $res == 3 ) {
    if ( intval($row['waittime']) > SysConfig::$MAXRUNTIME ) {
        $sql = "update solution set result=7, time=? where solution_id=?";
        pdo_query($sql, $row['waittime']*1000, $solution_id);

        $sql = "update runtimeinfo set `error`=? WHERE `solution_id`=?";
        pdo_query($sql, '请确认程序是否有等待或死循环', $solution_id);
        $runtime = $row['waittime'];
        $res = 7;
    }
}

if( isset($_GET['tr']) ) {
    // 编译错误
    if($res==11){
        $sql="SELECT `error` FROM `compileinfo` WHERE `solution_id`=?";
    }
    else{
        $sql="SELECT `error` FROM `runtimeinfo` WHERE `solution_id`=?";
    }
    $result=pdo_query($sql,$solution_id);
    $row=$result[0];
    if($row){
        echo htmlentities(str_replace("\n\r","\n",$row['error']),ENT_QUOTES,"UTF-8");
        // 删除自定义输入
        $sql="delete from custominput where solution_id=?";
        pdo_query($sql,$solution_id);
    }
}
else{
    echo $res.",".$row['memory'].",".$runtime.",".$row['judger'];
}

?>
